﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Orchard.Indexing;

namespace TekFlow.FamilyTree
{
    public class FamilyTreeDataMigration : DataMigrationImpl
    {
        public int Create()
        {
            SchemaBuilder.CreateTable("FamilyTreeRecord",
                table => table
                    .ContentPartRecord()
                    .Column<string>("FamilyName",c => c.WithLength(100))
                    .Column<string>("Url")
                    .Column<int>("FamilyMemberId")
                );

            SchemaBuilder.CreateTable("FamilyMemberRecord",
            table => table
                .Column<int>("Id", column => column.PrimaryKey().Identity())
                .Column<string>("name", c => c.WithLength(100))
                .Column<string>("url")
                .Column<decimal>("x")
                .Column<decimal>("y")
                .Column<int>("parentId")
            );

            ContentDefinitionManager.AlterPartDefinition(
               typeof(Models.FamilyTreePart).Name, cfg => cfg.Attachable());

            ContentDefinitionManager.AlterTypeDefinition("Tekflow FamilyTree Page", cfg => cfg
              .WithPart("BodyPart")
              .WithPart("CommonPart")
              .WithPart("MenuPart")
              .WithPart("PublishLaterPart")
              .WithPart("RoutePart")
              .WithPart("FamilyTreePart")
              .Creatable()
              .Indexed());

            return 1;
        }

        public int UpdateFrom1()
        {
           //Make our Part a widget as well
            ContentDefinitionManager.AlterTypeDefinition("TekflowFamilyTreeWidget", cfg => cfg
                .WithPart("FamilyTreePart")
                .WithPart("WidgetPart")
                .WithPart("CommonPart")
                .WithSetting("Stereotype", "Widget"));

            return 2;
        }

    }
}